package compress;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @program: WCount
 * @description: 测试压缩
 * @author: Qiang.Ye
 * @create: 2019-05-28 07:19
 **/
public class TestCompress {

    public static void main(String[] args) {

        //压缩
//        compress("","");

        //解压缩
//        decompress("","");

    }


    private static void compress(String fileName,String method) throws IOException, ClassNotFoundException {

        FileInputStream fis = new FileInputStream(new File(""));

        //通过反射找到对应的类
        Class<?> aClass = Class.forName(method);

        //通过反射器工具类找到编码器对象 & conf配置
        CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(aClass, new Configuration());

        //创建文件输出流
        FileOutputStream fos = new FileOutputStream(new File(fileName + codec.getDefaultExtension()));

        //获取编码器输出对象
        CompressionOutputStream cos = codec.createOutputStream(fos);

        //流拷贝
        IOUtils.copyBytes(fis,fos,5*1024*1024,false);

        cos.close();
        fos.close();
        fis.close();

    }


}
